#include "sysmm.h"
#include "mmu.h"
#include "proc.h"
#include "fs/fat32.h"
#include "fs/file.h"
#include "string.h"
address_t sysmm_brk(address_t brk)
{
    proc_t *p = getCurrentProc();
    if(p->heap == NULL)
    {
        void * mem = kalloc();
        create_pages_map(p->upageTable, (address_t)PRO_SIZE, (address_t)mem, PAGE_SIZE, PTE_R | PTE_W | PTE_U);
        create_pages_map(p->kpageTable, (address_t)PRO_SIZE, (address_t)mem, PAGE_SIZE, PTE_R | PTE_W);
        p->heap = mem;
    }
    p->heap = (void *)((address_t)p->heap + brk);
    return (address_t)p->heap;
}

int sysmm_munmap(void *start, size_t len)
{
    size_t size = sizeof(long int) / sizeof(char);
    memset(start, 0, len * size);
    return 0;
}

address_t sysmm_mmap(void *start, size_t len, int prot, int flags, int fd, uint64_t off)
{
    //TODO: get pa; deal with fd/off; prot/flags
    // extern pageTable_t kernel_pageTable;
    // address_t pa;
 
    // create_pages_map(kernel_pageTable, (address_t)start, (address_t)pa, len, PTE_R | PTE_W);
    proc_t *p = getCurrentProc();
    file_t *f = p->openFiles[fd];
    size_t size = sizeof(long int) / sizeof(char);
    __fat32_read((f->d_entry), 0, start, 0, len * size);
    return 0;
}